package cn.claim.storage.tools;

public final class NodeData implements JSONParsable {
    private final String ip;
    private final int uploadPort;
    private final int downloadPort;
    private final int queryPort;
    private long spareSpace;
    private long totalVolume;
    private long fileNum = 0;
    private final String name;
    private transient int survival = 2; //Second-chance

    NodeData(String ip, int uploadPort, int downloadPort, int queryPort, long spareSpace, String name, long totalVolume) {
        this.ip = ip;
        this.uploadPort = uploadPort;
        this.downloadPort = downloadPort;
        this.queryPort = queryPort;
        this.spareSpace = spareSpace;
        this.totalVolume = totalVolume;
        this.name = name;
    }

    public long getTotalVolume() {
        return totalVolume;
    }

    public long getFileNum() {
        return fileNum;
    }

    public void setFileNum(long fileNum) {
        this.fileNum = fileNum;
    }

    public void downSurvival() {
        survival--;
    }

    public void reset(NodeData nodeData){
        spareSpace = nodeData.getSpareSpace();
        fileNum = nodeData.getFileNum();
        survival = 2;
    }

    public boolean isDead() {
        return survival == 0;
    }

    public String getIp() {
        return ip;
    }

    public int getUploadPort() {
        return uploadPort;
    }

    public int getQueryPort() {
        return queryPort;
    }

    public int getDownloadPort() {
        return downloadPort;
    }

    public long getSpareSpace() {
        return spareSpace;
    }

    public void setSpareSpace(long spareSpace) {
        this.spareSpace = spareSpace;
    }

    @Override
    public String toString() {
        return this.toJSON();
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        NodeData nodeData = (NodeData) o;

        if (uploadPort != nodeData.uploadPort) return false;
        if (downloadPort != nodeData.downloadPort) return false;
        if (queryPort != nodeData.queryPort) return false;
        if (ip != null ? !ip.equals(nodeData.ip) : nodeData.ip != null) return false;
        return name != null ? name.equals(nodeData.name) : nodeData.name == null;
    }

    @Override
    public int hashCode() {
        int result = ip != null ? ip.hashCode() : 0;
        result = 31 * result + uploadPort;
        result = 31 * result + downloadPort;
        result = 31 * result + queryPort;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}
